package com.genesis.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genesis.mall.dto.PageDto;
import com.genesis.mall.entity.MemberProductCollection;
import com.genesis.mall.entity.UmsMember;
import com.genesis.mall.mapper.MemberProductCollectionMapper;
import com.genesis.mall.service.IMemberProductCollectionService;
import com.genesis.mall.service.IUmsMemberService;
import com.genesis.mall.utils.TokenUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Author: HShuai
 * @Date: 2022/08/03/20:42星期三
 * @Description:
 */
@Service
public class MemberProductCollectionServiceImpl extends ServiceImpl<MemberProductCollectionMapper, MemberProductCollection>
        implements IMemberProductCollectionService {

    @Autowired
    private IUmsMemberService umsMemberService;

    @Override
    public void saveProduct(MemberProductCollection memberProductCollection) {
        UmsMember umsMember = umsMemberService.getById(memberProductCollection.getMemberId());
        memberProductCollection.setCreateTime(LocalDateTime.now());
        memberProductCollection.setMemberNickname(umsMember.getNickname());
        save(memberProductCollection);

    }

    @Override
    public void clearById(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String substring = token.substring(6);
        Claims claims = TokenUtil.getClaims(substring);
        Long memberId = Long.parseLong(claims.get("umsMember").toString());

        LambdaQueryWrapper<MemberProductCollection> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberProductCollection::getMemberId,memberId);
        remove(lqw);
    }

    @Override
    public void deleteById(Long productId, HttpServletRequest request) {
        //获取用户id
        String token = request.getHeader("Authorization");
        String substring = token.substring(6);
        Claims claims = TokenUtil.getClaims(substring);
        Long memberId = Long.parseLong(claims.get("umsMember").toString());
        LambdaQueryWrapper<MemberProductCollection> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberProductCollection::getMemberId,memberId);
        lqw.eq(MemberProductCollection::getProductId,productId);
        remove(lqw);
    }

    @Override
    public MemberProductCollection getDetail(Long productId, HttpServletRequest request) {
        //获取用户id
        String token = request.getHeader("Authorization");
        String substring = token.substring(6);
        Claims claims = TokenUtil.getClaims(substring);
        Long memberId = Long.parseLong(claims.get("umsMember").toString());
        LambdaQueryWrapper<MemberProductCollection> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberProductCollection::getMemberId,memberId);
        lqw.eq(MemberProductCollection::getProductId,productId);
        return getOne(lqw);

    }

    @Override
    public PageDto<MemberProductCollection> pageList(HttpServletRequest request,Integer pageNum, Integer pageSize) {
        //获取用户id
        String token = request.getHeader("Authorization");
        String substring = token.substring(6);
        Claims claims = TokenUtil.getClaims(substring);
        Long memberId = Long.parseLong(claims.get("umsMember").toString());
        //创建分页构造器
        Page<MemberProductCollection> collectionPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<MemberProductCollection> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberProductCollection::getMemberId,memberId);
        page(collectionPage,lqw);
        PageDto<MemberProductCollection> pageDto = new PageDto<>();
        pageDto.setPageNum(pageNum);
        pageDto.setPageSize(pageSize);
        pageDto.setTotal((int) collectionPage.getTotal());
        pageDto.setTotalPage((int) collectionPage.getPages());
        pageDto.setList(collectionPage.getRecords());

        return pageDto;
    }
}
